<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .image-item {
            height: 300px;
            display: block;
            margin-bottom: 50px;
        }
    </style>
</head>

<body>
    <img lazyload='true' data-original="https://cdn.pixabay.com/photo/2020/04/15/04/11/lake-5045059__340.jpg" alt=""
        class="image-item">
    <img lazyload='true' data-original="https://cdn.pixabay.com/photo/2020/04/15/04/11/lake-5045059__340.jpg" alt=""
        class="image-item">
    <img lazyload='true' data-original="https://cdn.pixabay.com/photo/2020/04/15/04/11/lake-5045059__340.jpg" alt=""
        class="image-item">
    <img lazyload='true' data-original="https://cdn.pixabay.com/photo/2020/04/15/04/11/lake-5045059__340.jpg" alt=""
        class="image-item">
    <img lazyload='true' data-original="https://cdn.pixabay.com/photo/2020/04/15/04/11/lake-5045059__340.jpg" alt=""
        class="image-item">
    <img lazyload='true' data-original="https://cdn.pixabay.com/photo/2020/04/15/04/11/lake-5045059__340.jpg" alt=""
        class="image-item">
    <img lazyload='true' data-original="https://cdn.pixabay.com/photo/2020/04/15/04/11/lake-5045059__340.jpg" alt=""
        class="image-item">
    <img lazyload='true' data-original="https://cdn.pixabay.com/photo/2020/04/15/04/11/lake-5045059__340.jpg" alt=""
        class="image-item">
    <img lazyload='true' data-original="https://cdn.pixabay.com/photo/2020/04/15/04/11/lake-5045059__340.jpg" alt=""
        class="image-item">
    <img lazyload='true' data-original="https://cdn.pixabay.com/photo/2020/04/15/04/11/lake-5045059__340.jpg" alt=""
        class="image-item">
    <img lazyload='true' data-original="https://cdn.pixabay.com/photo/2020/04/15/04/11/lake-5045059__340.jpg" alt=""
        class="image-item">
    <img lazyload='true' data-original="https://cdn.pixabay.com/photo/2020/04/15/04/11/lake-5045059__340.jpg" alt=""
        class="image-item">
    <img lazyload='true' data-original="https://cdn.pixabay.com/photo/2020/04/15/04/11/lake-5045059__340.jpg" alt=""
        class="image-item">
    <img lazyload='true' data-original="https://cdn.pixabay.com/photo/2020/04/15/04/11/lake-5045059__340.jpg" alt=""
        class="image-item">
    <img lazyload='true' data-original="https://cdn.pixabay.com/photo/2020/04/15/04/11/lake-5045059__340.jpg" alt=""
        class="image-item">
    <img lazyload='true' data-original="https://cdn.pixabay.com/photo/2020/04/15/04/11/lake-5045059__340.jpg" alt=""
        class="image-item">
    <img lazyload='true' data-original="https://cdn.pixabay.com/photo/2020/04/15/04/11/lake-5045059__340.jpg" alt=""
        class="image-item">
    <img lazyload='true' data-original="https://cdn.pixabay.com/photo/2020/04/15/04/11/lake-5045059__340.jpg" alt=""
        class="image-item">
    <img lazyload='true' data-original="https://cdn.pixabay.com/photo/2020/04/15/04/11/lake-5045059__340.jpg" alt=""
        class="image-item">
    <img lazyload='true' data-original="https://cdn.pixabay.com/photo/2020/04/15/04/11/lake-5045059__340.jpg" alt=""
        class="image-item">
    <img lazyload='true' data-original="https://cdn.pixabay.com/photo/2020/04/15/04/11/lake-5045059__340.jpg" alt=""
        class="image-item">
    <img lazyload='true' data-original="https://cdn.pixabay.com/photo/2020/04/15/04/11/lake-5045059__340.jpg" alt=""
        class="image-item">
    <img lazyload='true' data-original="https://cdn.pixabay.com/photo/2020/04/15/04/11/lake-5045059__340.jpg" alt=""
        class="image-item">
    <img lazyload='true' data-original="https://cdn.pixabay.com/photo/2020/04/15/04/11/lake-5045059__340.jpg" alt=""
        class="image-item">
    <img lazyload='true' data-original="https://cdn.pixabay.com/photo/2020/04/15/04/11/lake-5045059__340.jpg" alt=""
        class="image-item">
    <img lazyload='true' data-original="https://cdn.pixabay.com/photo/2020/04/15/04/11/lake-5045059__340.jpg" alt=""
        class="image-item">
    <img lazyload='true' data-original="https://cdn.pixabay.com/photo/2020/04/15/04/11/lake-5045059__340.jpg" alt=""
        class="image-item">
</body>

<script>
    // 获取可视区域的高度
    let viewHeight = window.innerHeight

    function lazyLoad() {
        let imgItem = document.querySelectorAll('img[data-original][lazyload]')

        imgItem.forEach(item => {
            console.log(item);
            let rect = item.getBoundingClientRect()
            // console.log(rect);
            if (rect.top < viewHeight && rect.bottom >= 0) {
                item.src = item.dataset.original
                item.removeAttribute('data-original')
                item.removeAttribute('lazyLoad')
            }
        })
    }

    document.addEventListener('scroll', lazyLoad)
</script>

</html>